package com.tsinghuabigdata.edu.ddmath.module.onlineTutoring;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.tsinghuabigdata.edu.ddmath.MVPModel.RequestListener;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.activity.RoboActivity;
import com.tsinghuabigdata.edu.ddmath.bean.MyCourse;
import com.tsinghuabigdata.edu.ddmath.bean.Records;
import com.tsinghuabigdata.edu.ddmath.bean.UserDetailinfo;
import com.tsinghuabigdata.edu.ddmath.commons.http.HttpResponse;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.adapter.SelectQuestionsAdapter;
import com.tsinghuabigdata.edu.ddmath.bean.DayQuestions;
import com.tsinghuabigdata.edu.ddmath.bean.Question;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.adapter.SelectQuestionsBaseAdapter;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.UploadImage;
import com.tsinghuabigdata.edu.ddmath.util.AccountUtils;
import com.tsinghuabigdata.edu.ddmath.util.DataUtils;
import com.tsinghuabigdata.edu.ddmath.util.ToastUtils;
import com.tsinghuabigdata.edu.ddmath.view.LoadingPager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 选择题目页面
 * Created by Administrator on 2017/3/14.
 */

public class SelectQuestionsActivity extends RoboActivity implements View.OnClickListener,PullToRefreshBase.OnRefreshListener {

    private static final String TAG = "SelectQuestionsActivity";

    private LinearLayout          mLlBack;
    private RelativeLayout        mRlContent;
//    private RecyclerView   mRvSelectQuestions;
    private PullToRefreshListView mLvSelectQuestions;
    private Button                mBtnFinishSelect;
    private LoadingPager          mLoadingPager;


    private Context mContext;
    private TutorModel mTutorModel = new TutorModel();
//    private SelectQuestionsAdapter mAdatper;
    private SelectQuestionsBaseAdapter mAdatper;
    private String accessToken;
    private String studentId;
    private String classId;
    private int               pageNum  = 1;
    private int               pageSize = 7;
    private List<UploadImage> mList    = new ArrayList<UploadImage>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_questions);
        mContext = this;
        initViews();
        initData();
    }


    private void initViews() {
        mLlBack = (LinearLayout) findViewById(R.id.ll_back);
        mRlContent = (RelativeLayout) findViewById(R.id.rl_content);
        mLvSelectQuestions = (PullToRefreshListView) findViewById(R.id.lv_select_questions);
        mBtnFinishSelect = (Button) findViewById(R.id.btn_finish_select);
        mLoadingPager = (LoadingPager) findViewById(R.id.loadingPager);

        mLoadingPager.setTargetView(mRlContent);
        mLoadingPager.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLoadingPager.showLoading();
                queryUploadImages();
            }
        });
        mLvSelectQuestions.setOnRefreshListener(this);

//        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        mLvSelectQuestions.setLayoutManager(layoutManager);
        mLlBack.setOnClickListener(this);
        mBtnFinishSelect.setOnClickListener(this);
    }

    private void initData() {
        queryLoginInfo();
        //        analogData();
        queryUploadImages();
    }

    private void queryLoginInfo() {
        try {
            accessToken = AccountUtils.getLoginUser().getAccessToken();
            Log.i(TAG, "accessToken=" + accessToken);
            UserDetailinfo detailinfo = AccountUtils.getUserdetailInfo();
            classId = detailinfo.getJoinedClassInfoList().get(0).getClassId();
            studentId = detailinfo.getStudentId();
            Log.i(TAG, "classId=" + classId + "studentId=" + studentId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void queryUploadImages() {
        accessToken = "ssfwegre";
        studentId = "STU8B944699DEF04FE6B1BB09EFE4991488";
        classId = "20D893F83D1A465889C511DAA33FCB11";
        mTutorModel.queryUploadImages(accessToken, pageNum, pageSize, studentId, new RequestListener<List<UploadImage>>() {

            @Override
            public void onSuccess(List<UploadImage> res) {
                Log.i(TAG, "queryUploadImages onSuccess");
                if (res == null || res.size() == 0) {
                    mLoadingPager.showEmpty();
                }
                mList.addAll(res);
                mAdatper = new SelectQuestionsBaseAdapter(mContext, mList);
                mLvSelectQuestions.setAdapter(mAdatper);
                if (res.size() < pageSize) {
                    mLvSelectQuestions.setMode(PullToRefreshBase.Mode.DISABLED);
                } else {
                    mLvSelectQuestions.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
                }
                mLoadingPager.showTarget();
            }

            @Override
            public void onFail(HttpResponse<List<UploadImage>> response, Exception ex) {
                Log.i(TAG, "queryUploadImages onFail");
                mLoadingPager.showFault();
                //                analogData();
            }
        });
    }

    private void analogData() {
        List<DayQuestions> dayQuestionsList = new ArrayList<DayQuestions>();
        for (int i = 0; i < 6; i++) {
            DayQuestions dayQuestions = new DayQuestions();
            dayQuestions.setDay("2017年3月" + (7 - i) + "日");
            List<Question> list = new ArrayList<Question>();
            Random random = new Random();
            for (int j = 0; j < 2 + random.nextInt(15); j++) {
                Question question = new Question();
                list.add(question);
                question.setId(i + "ID" + j);
            }
            dayQuestions.setList(list);
            dayQuestionsList.add(dayQuestions);
        }
//        mAdatper = new SelectQuestionsAdapter(this, dayQuestionsList);
        mLvSelectQuestions.setAdapter(mAdatper);
        mLoadingPager.showTarget();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back:
                setResult(0);
                finish();
                break;
            case R.id.btn_finish_select:
                setResult(1);
                finish();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        setResult(0);
        finish();
    }

    @Override
    public void onRefresh(PullToRefreshBase pullToRefreshBase) {
        getMoreUploadImages();
    }

    //加载更多——拍照上传作业列表
    private void getMoreUploadImages() {
        pageNum++;
        mTutorModel.queryUploadImages(accessToken, pageNum, pageSize, studentId, new RequestListener<List<UploadImage>>() {

            @Override
            public void onSuccess(List<UploadImage> res) {
                Log.i(TAG, "getMoreUploadImages onSuccess");
                if (res == null || res.size() == 0) {
                    ToastUtils.showShort(mContext, R.string.server_error);
                    mLvSelectQuestions.onRefreshComplete();
                }
                mList.addAll(res);
                mAdatper.addMap();
                mAdatper.notifyDataSetChanged();
                if (res.size() < pageSize) {
                    mLvSelectQuestions.setMode(PullToRefreshBase.Mode.DISABLED);
                } else {
                    mLvSelectQuestions.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
                }
                pageNum++;
                mLvSelectQuestions.onRefreshComplete();
            }

            @Override
            public void onFail(HttpResponse<List<UploadImage>> response, Exception ex) {
                Log.i(TAG, "getMoreUploadImages getMoreUploadImages onFail");
                ToastUtils.showShort(mContext, R.string.server_error);
                mLvSelectQuestions.onRefreshComplete();
            }
        });
    }
}
